from itemadapter import ItemAdapter
import pymysql
from time import sleep
import datetime
fruits={}
try:
    conn = pymysql.Connection(host="192.168.0.113",port=3306,user='coname',password='xZS6p4LDcKCwn4Yb',db="co",charset="utf8")
    cursor = conn.cursor()
    print('连接数据库成功!')
except Exception as e:
    print("连接数据库失败!>>>",e)

#查找数据水果
a=cursor.execute('select distinct srname from co_fruit_detail')
print(a)
i=1
try:
    cursor.execute('select distinct srname,pid from co_fruit_detail')
    res = cursor.fetchall()
    
except Exception as e:
    print('查找水果名称失败')
#将水果名称和水果id插入数据库
try:
    for res0 in res:
        res1=''.join(res0[0])
        res2=''.join(res0[1])
        cursor.execute('insert into co_fruit (srname,keywordid) values("{}","{}")'.format(res1,res2))
        conn.commit()
        print("插入"+res1+"成功")
except Exception as e:
    print('插入数据失败!>>>',e)
try:
    cursor.close()
    conn.close()
    print('关闭数据库成功!')
except Exception as e:
    print('连接数据库失败>>>',e)